<?php

App::uses('AppController', 'Controller');

/**
 * Adminrosters Controller
 *
 * @property Adminroster $Adminroster
 */
class AdminrostersController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index($time = null) {

        if ($time == null) {
            $timeset = time();
        } else if ($time == 0) {
            $timeset = '-1 month';
        } else if ($time == 1) {
            $timeset = time();
        } else {
            $timeset = '+1 month';
        }
        $this->set(compact('timeset'));

        $this->loadModel('Time');
        $times = $this->Time->find('all');
        $this->set(compact('times'));

        $this->loadModel('User');
        $users = $this->User->find('all');
        $this->set(compact('users'));

        $this->loadModel('Roster');

        $this->loadModel('Adminroster');
        $adminrosters = $this->Adminroster->find('list', array('fields' => array('Adminroster.ros_id')));
        $rosters = $this->Roster->find('all', array('conditions' => array('ros_id' => $adminrosters)));
        // debug($rosters);
        $this->set(compact('rosters'));
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Adminroster->exists($id)) {
            throw new NotFoundException('Invalid adminroster','default',array('class'=>'alert alert-danger'));
        }
        $options = array('conditions' => array('Adminroster.' . $this->Adminroster->primaryKey => $id));
        $this->set('adminroster', $this->Adminroster->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->Adminroster->create();
            if ($this->Adminroster->save($this->request->data)) {
                $this->flash('Adminroster saved.','default',array('class'=>'alert alert-info'), array('action' => 'index'));
            } else {
                
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->Adminroster->exists($id)) {
            throw new NotFoundException('Invalid adminroster','default',array('class'=>'alert alert-danger'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Adminroster->save($this->request->data)) {
                $this->flash('The adminroster has been saved.','default',array('class'=>'alert alert-info'), array('action' => 'index'));
            } else {
                
            }
        } else {
            $options = array('conditions' => array('Adminroster.' . $this->Adminroster->primaryKey => $id));
            $this->request->data = $this->Adminroster->find('first', $options);
        }
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->Adminroster->id = $id;
        if (!$this->Adminroster->exists()) {
            throw new NotFoundException('Invalid adminroster','default',array('class'=>'alert alert-danger'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Adminroster->delete()) {
            $this->flash('Adminroster deleted','default',array('class'=>'alert alert-info'), array('action' => 'index'));
        }
        $this->flash('Adminroster was not deleted','default',array('class'=>'alert alert-danger'), array('action' => 'index'));
        $this->redirect(array('action' => 'index'));
    }

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index', 'view', 'add', 'edit', 'delete', 'submit');
    }

    public function assign($time = null) {
        if ($time == null) {
            $timeset = time();
        } else if ($time == 0) {
            $timeset = '-1 month';
        } else if ($time == 1) {
            $timeset = time();
        } else {
            $timeset = '+1 month';
        }
        $this->set(compact('timeset'));

        $this->loadModel('Time');
        $times = $this->Time->find('all');
        $this->set(compact('times'));

        $this->loadModel('Roster');
        $rosters = $this->Roster->find('all');
        $this->set(compact('rosters'));

        $this->loadModel('User');
        $users = $this->User->find('all');
        $this->set(compact('users'));
        //$selectuser = $this->User->find('all', array('conditions' => array('user_id' => $rosters)));
        //debug($rosters);
        //die;
        //$this->set(compact($selectuser));

        $this->loadModel('Adminroster');
        $adminrosters = $this->Adminroster->find('all');
        $this->set(compact('adminrosters'));
    }

    public function save() {
        $monthyear = $this->request->data['adminrosters']['monthyear'];
        $test = $this->request->data('option');
        $this->loadModel('Roster');

        //debug($test);
        $this->Adminroster->deleteAll(array('Adminroster.monthyear' => $monthyear));
        $this->Roster->updateAll(array('Roster.isapprove' => '0'));
        
        foreach ($test as $testing):
            $this->Roster->updateAll(
                    array('Roster.isapprove' => '1'), array('Roster.ros_id' => $testing));

            $this->Adminroster->set('ros_id', $testing);
            $this->Adminroster->set('monthyear', $monthyear);
            $this->Adminroster->saveAll();
        endforeach;
        
        $adminrosters = $this->Adminroster->find('list',array('fields' => array('Adminroster.ros_id')));
        $this->Roster->updateAll(array('Roster.isapprove' => '1'), array('Roster.ros_id' => $adminrosters));
            
        
        foreach ($adminrosters as $adminroster){
             //$this->Roster->updateAll(array('Roster.isapprove' => '1', array('Roster.ros_is' => $adminroster['Adminroster']['ros_id'])));
            
        }

        //debug($result);
        $this->redirect('assign');
        //debug($test);
        //die;
    }

    public function staff() {
        $time = $this->request->query['time'];
        $date = $this->request->query['date'];
//         debug($time);
//         debug($date);

        $this->loadModel('User');
        $users = $this->User->find('all');
        $this->set(compact('users'));

        $this->loadModel('Roster');
        $rosters = $this->Roster->find('list', array('conditions' => array('Roster.ros_date' => $date, 'Roster.time_id' => $time), 'fields' => array('Roster.ros_userid')));
        $users = $this->User->find('list', array('conditions' => array('NOT' => array('User.user_id' => $rosters)), 'fields' => array('User.user_id', 'User.staff_name')));
        $this->set(compact('users'));
        $this->set('time', $time);
        $this->set('date', $date);
//        debug($users);
//        die;
//        $this->loadModel('Adminroster');
//        $adminrosters = $this->Adminroster->find('all');
//        $this->set(compact('adminrosters'));
    }

    public function saveStaff() {
        $staff = $this->request->data;
//        debug($staff['Adminroster']['date']);
        //debug($staff['Adminroster']['Staff List']);
//        die;

        $this->loadModel('Roster');

        foreach ($staff['Adminroster']['Staff List'] as $staffID) {

            $this->Roster->create();
            $this->Roster->set('time_id', $staff['Adminroster']['time']);
            $this->Roster->set('ros_date', $staff['Adminroster']['date']);
            $this->Roster->set('isapprove', '1');
            $this->Roster->set('ros_userid', $staffID);
            $this->Roster->saveAll();
            $this->Adminroster->set('ros_id', $this->Roster->getLastInsertID());
            $this->Adminroster->set('monthyear',$staff['Adminroster']['monthyear']);
            $this->Adminroster->saveAll();
        }




        $this->redirect('assign');
    }

}
